﻿using Microsoft.AspNetCore.Mvc;
using Shop.Core.Share;
using Shop.IServices;
using Shop.Model.Dto.User;
using Shop.Model.View.Use;
using System.Text.RegularExpressions;

namespace Shop.WebApi.Controllers
{
    /// <summary>
    /// 用户个人信息服务
    /// </summary>
    [ApiController]
    [Route("Api/[controller]")]
    public class UserController : ShopControllerBase
    {
        private readonly IUserAppService _userAppService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userAppService"></param>
        public UserController(IUserAppService userAppService)
        {
            _userAppService = userAppService;
        }

        /// <summary>
        /// 个人信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("BaseInfor")]
        public async Task<AppResult<UserInforVo>> GetUserBaseInfor()
            => await _userAppService.GetUserBaseInfor();

        /// <summary>
        /// 添加上级
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("AddSuperior")]
        public async Task<AppResult> AddSuperior(SimpleDto<int> input)
        {
            try
            {
                if (input.Value <= 50000)
                    return Problem("上级不存在");
                var result = await _userAppService.AddSuperior(input.Value);
                if (result)
                    return Success();
                else
                    return Problem("系统错误");
            }
            catch (Exception ex)
            {
                return Problem(ex.Message);
            }
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost("SendChangeCode")]
        public async Task<AppResult> SendChangeCode()
            => await _userAppService.SendChangeCode();

        /// <summary>
        /// 检查验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost("CheckChangeCode")]
        public async Task<AppResult> CheckChangeCode(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value) || !Regex.IsMatch(input.Value, @"^\d{4}$"))
                return Problem("验证码格式错误");
            var result = await _userAppService.CheckChangeCode(input.Value);
            if (result)
                return Success();
            else
                return Problem("验证码错误");

        }

        /// <summary>
        /// 修改支付密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("ChangeTradePassword")]
        public async Task<AppResult> ChangeTradePassword(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value) || !Regex.IsMatch(input.Value, @"^\d{6}$"))
                return Problem("支付密码格式错误");
            var result = await _userAppService.ChangeTradePassword(input.Value);
            if (result)
                return Success();
            else
                return Problem("支付密码修改失败");
        }

        /// <summary>
        /// 修改登录密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("ChangePassword")]
        public async Task<AppResult> ChangePassword(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value) || !Regex.IsMatch(input.Value, @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"))
                return Problem("密码格式错误");
            var result = await _userAppService.ChangePassword(input.Value);
            if (result)
                return Success();
            else
                return Problem("密码修改失败");
        }

        /// <summary>
        /// 验证交易密码是否设置
        /// </summary>
        /// <returns></returns>
        [HttpGet("VerifyTPW")]
        public async Task<AppResult<bool>> VerifyTradePassworde()
            => await _userAppService.VerifyTradePassworde();

        /// <summary>
        /// 更改手机号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("ChangeTelephone")]
        public async Task<AppResult> ChangeTelephone(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value) || !Regex.IsMatch(input.Value, @"^1[3-9]\d{9}$"))
                return Problem("手机号码错误");
            try
            {
                var result = await _userAppService.ChangeTelephone(input.Value);
                if (result)
                    return Success();
                else
                    return Problem("手机号码更改失败");
            }
            catch (Exception ex)
            {
                return Problem(ex.Message);
            }
        }

        /// <summary>
        /// 账号注销
        /// </summary>
        /// <returns></returns>
        [HttpPost("Cancel")]
        public async Task<AppResult> AccountCancellation()
        {
            var result = await _userAppService.AccountCancellation();
            if (result)
                return Success();
            else
                return Problem("账号已注销");
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("UpdateUserInfor")]
        public async Task<AppResult> UpdateUserInforByWeChat(UserWechatDto input)
            => await _userAppService.UpdateUserInforByWeChat(input);

        /// <summary>
        /// 更新用户名称
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("UpdateName")]
        public async Task<AppResult> UpdateUserName(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value))
                return Problem("用户昵称不能为空");
            if (input.Value.Length > 20)
                return Problem("用户昵称长度太长");
            var result = await _userAppService.UpdateUserName(input.Value);
            if (result)
                return Success();
            else
                return Problem("更新用户昵称失败");
        }

        /// <summary>
        /// 更新用户头像
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("UpdateHead")]
        public async Task<AppResult> UpdateUserHead(SimpleDto<string> input)
        {
            if (string.IsNullOrEmpty(input.Value))
                return Problem("请上传用户头像");
            var result = await _userAppService.UpdateUserHead(input.Value);
            if (result)
                return Success();
            else
                return Problem("更新用户头像失败");
        }

        /// <summary>
        /// 添加合同
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("UploadContract")]
        public async Task<AppResult> UploadContract([FromBody]AddContractDto input)
            => await _userAppService.Contract(input);
    }
}
